<template>
  <el-dialog title="绑定员工" :visible.sync="openEmployees" width="600px" append-to-body>
    <el-form :model="queryParams" size="small" inline @submit.native.prevent>
      <el-form-item>
        <el-input
            v-model="queryParams.combination"
            placeholder="搜索姓名或工号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="employeesList">
      <el-table-column width="50" align="center">
        <template slot-scope="{row}">
          <el-radio v-model="radio" :kwy="row.id" :label="row.id">123</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="staffName"/>
      <el-table-column label="所属部门" align="center" prop="department"/>
      <el-table-column label="编号" align="center" prop="jobNumber"/>
      <el-table-column label="身份证号" align="center" prop="idCardNumber"/>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="curPage"
        :limit.sync="pagesize"
        :pageSizes="[5,10,15,20]"
        @pagination="getEmployeesList"
    ></pagination>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitFormEmployees" :disabled="!radio">确 定</el-button>
      <el-button @click="cancelEmployees">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getEmployeesList } from '@/api/system/user'

export default {
  name: 'selectEmployees',
  props: ['openEmployees', 'id'],
  data() {
    return {
      employeesList: [],
      queryParams: { combination: '' },
      curPage: 1,
      radio: '',
      total: 0,
      pagesize: 5
    }
  },
  watch: {
    openEmployees: {
      handler: function(v) {
        if (v) {
          this.getEmployeesList(true)
        } else {
          this.curPage = 1
          this.queryParams = { combination: '' }
        }
      },
      immediate: true
    },
    id: {
      handler: function(v) {
        this.radio = v
      },
      immediate: true
    }
  },
  methods: {
    getEmployeesList(v) {
      if (typeof v === 'boolean') {
        this.curPage = 1
      }
      getEmployeesList({ ...this.queryParams, pageSize: this.pagesize, curPage: this.curPage }).then(res => {
        this.employeesList = res.data.records
        this.total = res.data.total
      })
    },
    submitFormEmployees() {
      this.$emit('submitFormEmployees', this.radio)
      this.cancelEmployees()
    },
    cancelEmployees() {
      this.$emit('update:openEmployees', false)
    },
    handleQuery() {
      this.getEmployeesList(true)
    },
    resetQuery() {
      this.queryParams = { combination: '' }
      this.getEmployeesList(true)
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-radio__label {
  display: none;
}
</style>
